<template>
  <div class="box">
    <hr id="hr" class="hr" />
    <video
      id="videoCamera"
      class="video"
      autoplay
      :width="videoWidth"
      :height="videoHeight"
    >
      <hr id="hr" class="hr" />
    </video>
    <canvas
      style="display: none"
      id="canvasCamera"
      :width="videoWidth"
      :height="videoHeight"
    >
    </canvas>
    <div v-if="imgSrc" class="img_bg_camera" id="img_bg_camera">
      <p class="text">效果预览</p>
      <img :src="imgSrc" alt class="tx_img" />
    </div>
    <div class="right-box" id="curve_show"></div>
    <div class="jindu" id="jindu">
      <div class="jindu-childen"></div>
    </div>
    <div class="button">
      <!-- <el-button @click="getCompetence()" type="success" size="mini" plain >打开摄像头</el-button> -->
      <el-button
        @click="setImage()"
        ref="setImage"
        id="setImage"
        class="setImage"
        type="success"
        size="small"
        plain
        >重新1识别</el-button
      >
      <el-button
        @click="setImage2()"
        ref="setImage"
        id="setImage2"
        class="setImage2"
        type="success"
        size="small"
        plain
        >重新识别</el-button
      >
      <!-- <el-button @click="stopNavigator()" type="success" size="mini" plain>关闭摄像头</el-button> -->
    </div>
    <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
    <div>
      <router-view v-if="this.imgSrc != ''"></router-view>
    </div>
  </div>
</template>

<script>
import $ from "jquery";
import * as echarts from "echarts";
export default {
  data() {
    return {
      videoWidth: 500,
      videoHeight: 350,
      imgSrc: "",
      thisCancas: null,
      thisContext: null,
      thisVideo: null,
      openVideo: false,
      number: 5,
    };
  },
  mounted() {
    this.initCurve();

    this.getCompetence(); //进入页面就调用摄像头
    // setImage:function(){
    //     console.log('爆炸了');
    //     var timer2=setTimeout(callback,5000);
    // }
    // var ad=document.getElementById('setImage');
    setTimeout(function () {
      // console.log(":@")
      // setImage()
      // this.$refs.setImage.click()
      document.getElementById("setImage").click();
      var videoCamera = document.getElementById("videoCamera");
      var hr = document.getElementById("hr");
      var jindu = document.getElementById("jindu");
      var setImage2 = document.getElementById("setImage2");
      //  img_bg_camera.className="img_bg_camera2"
      // img_bg_camera.setAttribute("className", "img_bg_camera2")
      videoCamera.style.display = "none";
      hr.style.display = "none";
      jindu.style.display = "none";
      $(".img_bg_camera").toggleClass("img_bg_camera2");
      setImage2.style.display = "inline";
    }, 5000);
  },
  methods: {
    // 调用权限（打开摄像头功能）
    getCompetence() {
      var _this = this;
      _this.thisCancas = document.getElementById("canvasCamera");
      _this.thisContext = this.thisCancas.getContext("2d");
      _this.thisVideo = document.getElementById("videoCamera");
      _this.thisVideo.style.display = "block";
      // 获取媒体属性，旧版本浏览器可能不支持mediaDevices，我们首先设置一个空对象
      if (navigator.mediaDevices === undefined) {
        navigator.mediaDevices = {};
      }
      // 一些浏览器实现了部分mediaDevices，我们不能只分配一个对象
      // 使用getUserMedia，因为它会覆盖现有的属性。
      // 这里，如果缺少getUserMedia属性，就添加它。
      if (navigator.mediaDevices.getUserMedia === undefined) {
        navigator.mediaDevices.getUserMedia = function (constraints) {
          // 首先获取现存的getUserMedia(如果存在)
          var getUserMedia =
            navigator.webkitGetUserMedia ||
            navigator.mozGetUserMedia ||
            navigator.getUserMedia;
          // 有些浏览器不支持，会返回错误信息
          // 保持接口一致
          if (!getUserMedia) {
            //不存在则报错
            return Promise.reject(
              new Error("getUserMedia is not implemented in this browser")
            );
          }
          // 否则，使用Promise将调用包装到旧的navigator.getUserMedia
          return new Promise(function (resolve, reject) {
            getUserMedia.call(navigator, constraints, resolve, reject);
          });
        };
      }
      var constraints = {
        audio: false,
        video: {
          width: this.videoWidth,
          height: this.videoHeight,
          transform: "scaleX(-1)",
        },
      };
      navigator.mediaDevices
        .getUserMedia(constraints)
        .then(function (stream) {
          // 旧的浏览器可能没有srcObject
          if ("srcObject" in _this.thisVideo) {
            _this.thisVideo.srcObject = stream;
          } else {
            // 避免在新的浏览器中使用它，因为它正在被弃用。
            _this.thisVideo.src = window.URL.createObjectURL(stream);
          }
          _this.thisVideo.onloadedmetadata = function (e) {
            _this.thisVideo.play();
          };
        })
        .catch((err) => {
          console.log(err);
        });
      alert("打开摄像头");
      //   this.$router.push('/shibie/paizhao/appleDesease')
    },
    //  绘制图片（拍照功能）
    setImage() {
      //     var videoCamera=document.getElementById("videoCamera")
      //     var hr=document.getElementById("hr")
      //     var jindu=document.getElementById("jindu");
      //     // var setImage=document.getElementById("setImage");
      //    //  img_bg_camera.className="img_bg_camera2"
      //    // img_bg_camera.setAttribute("className", "img_bg_camera2")
      //    videoCamera.style.display="inline";
      //    hr.style.display="iline";
      //    jindu.style.display="inline";
      // setImage.style.display="inline"
      // setTimeout(function(){
      var _this = this;
      // canvas画图
      _this.thisContext.drawImage(
        _this.thisVideo,
        0,
        0,
        _this.videoWidth,
        _this.videoHeight
      );
      // 获取图片base64链接
      var image = this.thisCancas.toDataURL("image/png");
      _this.imgSrc = image; //赋值并预览图片
      // $('.img_bg_camera').toggleClass('img_bg_camera4');
      // },5000)
      // $('.img_bg_camera').toggleClass('img_bg_camera2');
      //   alert("拍照");x    xxxxx
    },
    setImage2() {
      var img_bg_camera = document.getElementById("img_bg_camera");
      img_bg_camera.style.display = "none";
      // $('.img_bg_camera').toggleClass('img_bg_camera3');
      var videoCamera = document.getElementById("videoCamera");
      var hr = document.getElementById("hr");
      var jindu = document.getElementById("jindu");

      // var img_bg_camera=document.getElementById("img_bg_camera");
      // $('.img_bg_camera').toggleClass('img_bg_camera2');
      // var setImage=document.getElementById("setImage");
      //  img_bg_camera.className="img_bg_camera2"
      // img_bg_camera.setAttribute("className", "img_bg_camera2")
      //  $('.img_bg_camera').toggleClass('img_bg_camera4');
      videoCamera.style.display = "inline";
      hr.style.display = "inline";
      jindu.style.display = "inline";
      $(".hr").toggleClass("hr2");
      $(".jindu").toggleClass("jindu2");
      //  img_bg_camera.style.display="none";
      //  img_bg_camera.setAttribute("className", "img_bg_camera")
      // setImage.style.display="inline"
      //    $('.img_bg_camera').toggleClass('img_bg_camera2');
      setTimeout(function () {
        var videoCamera = document.getElementById("videoCamera");
        var hr = document.getElementById("hr");
        var jindu = document.getElementById("jindu");
        var img_bg_camera = document.getElementById("img_bg_camera");
        videoCamera.style.display = "none";
        hr.style.display = "none";
        jindu.style.display = "none";
        var img_bg_camera = document.getElementById("img_bg_camera");
        img_bg_camera.style.display = "inline";
        // img_bg_camera.setAttribute("className", "img_bg_camera2")
        // this.setImage()
        $(".hr").toggleClass("hr");
        $(".jindu").toggleClass("jindu");
        document.getElementById("setImage").click();
        // $('.img_bg_camera').toggleClass('img_bg_camera4');
        // setImage()
      }, 5000);
    },
    // 关闭摄像头
    stopNavigator() {
      this.thisVideo.srcObject.getTracks()[0].stop();
      alert("关闭摄像头");
    },
    initCurve() {
      // console.log(11)
      var myChart = echarts.init(document.getElementById("curve_show"));
      myChart.setOption({
        // title:'病 害 检 测 结置信度TOP',
        title: {
          text: "病 害 检 测 结置信度TOP",
        },
        xAxis: {
          type: "category",
          data: ["Mon", "Tue", "Wed"],
        },
        yAxis: {
          type: "value",
        },
        series: [
          {
            data: [70, 20, 10],
            type: "bar",
            showBackground: true,
            backgroundStyle: {
              color: "rgba(180, 180, 180, 0.2)",
            },
          },
        ],
      });
    },
  },
};
</script>
<style scoped>
.box {
  width: 1100px;
  height: 420px;
  background-color: rgb(255, 255, 255);
  margin: auto;
  position: relative;
}
.video {
  /* width:400px;
    height:400px; */
  float: left;
  /* position: relative; */
  margin-top: -2px;
  position: relative;
  margin-left: 65px;
}
.button {
  position: absolute;
  margin-top: 360px;
  margin-left: 245px;
}
.text {
  position: absolute;
  left: 40px;
}
.img_bg_camera {
  width: 350px;
  height: 400px;
  float: left;
  position: relative;
  margin-left: 75px;
  margin-top: -2px;
}
.img_bg_camera2 {
  width: 350px;
  height: 400px;
  float: left;
  position: relative;
  margin-left: 75px;
  margin-top: -2px;
}
.img_bg_camera3 {
  /* width:350px;
    height:400px;
    float:left;
    position: relative;
    margin-left: 75px;
    margin-top: -2px; */
  display: none;
}
.img_bg_camera4 {
  width: 350px;
  height: 400px;
  float: left;
  position: relative;
  margin-left: 75px;
  margin-top: -2px;
  display: inline;
}
.right-box {
  width: 396px;
  height: 400px;
  background-color: rgb(255, 255, 255);
  float: right;
  margin-top: 10px;
}
.hr {
  width: 600px;
  height: 350px;
  animation: w 2s steps(10) forwards;
  animation-timing-function: linear;
  animation-direction: alternate;
  animation-iteration-count: infinite;
  z-index: 1;
  position: absolute;
  top: -10px;
  float: left;
  margin-left: 65px;
}
.hr2 {
  width: 600px;
  height: 350px;
  animation: w 2s steps(10) forwards;
  animation-timing-function: linear;
  animation-direction: alternate;
  animation-iteration-count: infinite;
  z-index: 1;
  position: absolute;
  top: -10px;
  float: left;
  margin-left: -500px;
}
.jindu {
  width: 500px;
  height: 10px;
  background-color: rgb(214, 223, 223);
  position: absolute;
  margin-top: 349px;

  margin-left: 65px;
}
.jindu2 {
  width: 500px;
  height: 10px;
  background-color: rgb(214, 223, 223);
  position: absolute;
  margin-top: 349px;

  margin-left: -500px;
}
.jindu-childen {
  width: 500px;
  height: 10px;
  background-color: blue;
  animation: w2 5s steps(5) forwards;
  animation-timing-function: linear;
  animation-direction: alternate;
  animation-iteration-count: 1;
}
.setImage {
  display: none;
  margin-left: 15px;
}
.setImage2 {
  display: none;
  margin-left: 15px;
}
.number {
  position: absolute;
  width: 200px;
  height: 200px;
  top: 40;
  left: 200;
  background-color: aqua;
}
@keyframes w {
  0% {
    width: 0px;
  }
  20% {
    width: 100px;
  }
  40% {
    width: 200px;
  }
  60% {
    width: 300px;
  }
  80% {
    width: 400px;
  }
  100% {
    width: 500px;
  }
}
@keyframes w2 {
  0% {
    width: 0px;
  }
  20% {
    width: 100px;
  }
  40% {
    width: 200px;
  }
  60% {
    width: 300px;
  }
  80% {
    width: 400px;
  }
  100% {
    width: 500px;
  }
}
</style>
